#include"mainscence.h"
#include "ui_mainscence.h"
#include"config.h"
#include<QIcon>
#include "kaishijiemian.h"
mainScence::mainScence(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::mainScence)
{
    ui->setupUi(this);
    initScene();
    playGame();
    QTimer m_Timer;
}

mainScence::~mainScence()
{
    delete ui;
}

void mainScence::initScene()
{
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    setWindowTitle(GAME_TITLE);
    setWindowIcon(QIcon(GAME_ICON));
    m_Timer.setInterval(GAME_RATE);
    m_recorder_com=0;
    m_recorder_buc=0;
    m_recorder_con=0;
    m_recorder3=BUCKETZOMB_INTERVAL;
    m_recorder2=CONEZOMB_INTERVAL;
    m_recorder1=COMMONZOMB_INTERVAL;

    OVER=false;
    WON=false;
    End.load(OVER_PATH);
    Won.load(WON_PATH);

    //加一个随机数种子
    srand((unsigned int )time(NULL));
    player->setSource(QUrl::fromLocalFile(":/plane/res/Grazy Dave.mp3"));
    player->setLoopCount(QSoundEffect::Infinite);
    player->setVolume(0.5);
}

void mainScence::playGame()
{
    m_Timer.start();

    connect(&m_Timer,&QTimer::timeout,[=](){
        //更新游戏元素坐标；
        commonzomb_toscene();

        bucketzomb_toscene();

        conezome_toscene();

        updatePosition();

        end();

        win();

        ui->score->setText("score: "+QString::number(m_Plant.m_score));
        update();

        collisionDetetion();

    });

}

void mainScence::updatePosition()
{
    m_map.mapPosition();

    //temp.m_Free=false;
    //temp.updatePosition();
    m_Plant.shot();

    for(int i=0;i<BULLET1_NUM;i++){
        if(m_Plant.m_bullet1s[i].m_Free==false){
            m_Plant.m_bullet1s[i].updatePosition();
        }
    }
    //敌机出场
    for(int i=0;i<COMMONZOMB_NUM;i++){
        if(m_commonzombs[i].m_Free==false){
            m_commonzombs[i].updatePosition();
        }

    }

    for(int i=0;i<BUCKETZOMB_NUM;i++){
        if(m_bucketzombs[i].m_Free==false){
            m_bucketzombs[i].updatePosition();
        }

    }
    for(int i=0;i<CONEZOMB_NUM;i++){
        if(m_conezombs[i].m_Free==false){
            m_conezombs[i].updatePosition();
        }

    }
    for(int i=0;i<BOMB_NUM;i++)
    {
        if(m_bombs[i].m_Free==false)
        {
            m_bombs[i].updateInfo();
        }
    }

}

void mainScence::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    //painter.drawPixmap(m_map.m_map1_posX,0,m_map.m_map1);
    painter.drawPixmap(m_map.m_map2_posX,0,m_map.m_map2);
    //绘制飞机
    painter.drawPixmap(m_Plant.m_X,m_Plant.m_Y,m_Plant.m_Plane);

    //绘制子弹
    for(int i=0;i<BULLET1_NUM;i++){
        if(m_Plant.m_bullet1s[i].m_Free==false){
                painter.drawPixmap(m_Plant.m_bullet1s[i].m_X,m_Plant.m_bullet1s[i].m_Y+m_bullet1.m_Bullet1.height(),m_Plant.m_bullet1s[i].m_Bullet1);
        }
    }

    //绘制敌机
    //普通僵尸
    for(int i=0;i<COMMONZOMB_NUM;i++){
        if(m_commonzombs[i].m_Free==false){
            painter.drawPixmap(m_commonzombs[i].m_X,m_commonzombs[i].m_Y,m_commonzombs[i].m_commonzomb);
        }

    }
    //铁桶僵尸
    for(int i=0;i<BUCKETZOMB_NUM;i++){
        if(m_bucketzombs[i].m_Free==false){
            painter.drawPixmap(m_bucketzombs[i].m_X,m_bucketzombs[i].m_Y,m_bucketzombs[i].m_bucketzomb);
        }

    }
    //路障僵尸
    for(int i=0;i<CONEZOMB_NUM;i++){
        if(m_conezombs[i].m_Free==false){
            painter.drawPixmap(m_conezombs[i].m_X,m_conezombs[i].m_Y,m_conezombs[i].m_conezomb);
        }

    }
    //绘制爆炸
    for(int i=0;i<BOMB_NUM;i++)
    {
        if(m_bombs[i].m_Free==false)
        {
            painter.drawPixmap(m_bombs[i].m_X,m_bombs[i].m_Y,m_bombs[i].m_pixarr[m_bombs[i].m_index]);
        }
    }
    //painter.drawPixmap(temp.m_X,temp.m_Y,temp.m_Bullet1);

    if(OVER)
    {
        painter.drawPixmap((GAME_WIDTH-End.width())*0.5,(GAME_HEIGHT-End.height())*0.5,End);
    }
    if(WON)
    {
        painter.drawPixmap((GAME_WIDTH-Won.width())*0.5,(GAME_HEIGHT-Won.height())*0.5,Won);
    }
}
void mainScence::bucketzomb_toscene()
{
    m_recorder_buc++;
    if(m_recorder_buc<m_recorder3)
    {
        return;
    }
    m_recorder_buc=0;
    for(int i=0;i<BUCKETZOMB_NUM;i++){
        if(m_bucketzombs[i].m_Free)
        {
            m_bucketzombs[i].m_Free=false;

            m_bucketzombs[i].m_X=GAME_WIDTH-m_bucketzombs[i].m_Rect.width();
            m_bucketzombs[i].m_Y=rand()%(GAME_HEIGHT-m_bucketzombs[i].m_Rect.height());
            break;
        }
    }
}

void mainScence::conezome_toscene()
{
    m_recorder_con++;
    if(m_recorder_con<m_recorder2)
    {
        return;
    }
    m_recorder_con=0;
    for(int i=0;i<CONEZOMB_NUM;i++){
        if(m_conezombs[i].m_Free)
        {
            m_conezombs[i].m_Free=false;

            m_conezombs[i].m_X=GAME_WIDTH-m_conezombs[i].m_Rect.width();
            m_conezombs[i].m_Y=rand()%(GAME_HEIGHT-m_conezombs[i].m_Rect.height());
            break;
        }
    }
}
void mainScence::commonzomb_toscene()
{
    m_recorder_com++;
    if(m_recorder_com<m_recorder1)
    {
        return;
    }
    m_recorder_com=0;
    for(int i=0;i<COMMONZOMB_NUM;i++){
        if(m_commonzombs[i].m_Free)
        {
            m_commonzombs[i].m_Free=false;

            m_commonzombs[i].m_X=GAME_WIDTH-m_commonzombs[i].m_Rect.width();
            m_commonzombs[i].m_Y=rand()%(GAME_HEIGHT-m_commonzombs[i].m_Rect.height());
            break;
        }
    }
}
//碰撞检测
void mainScence::collisionDetetion()
{
    for(int i=0;i<COMMONZOMB_NUM;i++)
    {
        if(m_commonzombs[i].m_Free)
        {
            continue;
        }
        for(int j=0;j<BULLET1_NUM;j++)
        {
            if(m_Plant.m_bullet1s[j].m_Free)
            {
                continue;
            }

            if(m_commonzombs[i].m_Rect.intersects(m_Plant.m_bullet1s[j].m_Rect))
            {

                m_Plant.m_bullet1s[j].m_Free=true;
                m_commonzombs[i].m_HP-=bullet.m_injury;
                if(m_commonzombs[i].m_HP<=0){
                m_commonzombs[i].m_Free=true;
                    m_commonzombs[i].m_HP=1;
                   m_Plant.m_score+=1;
                for(int k=0;k<BOMB_NUM;k++){
                    if(m_bombs[k].m_Free)
                    {
                        m_bombs[k].m_Free=false;

                        m_bombs[k].m_X=m_commonzombs[i].m_X;
                        m_bombs[k].m_Y=m_commonzombs[i].m_Y;
                        break;
                    }

                }
                }
                }
        }

    }

    for(int i=0;i<CONEZOMB_NUM;i++)
    {
        if(m_conezombs[i].m_Free)
        {
            continue;
        }
        for(int j=0;j<BULLET1_NUM;j++)
        {
            if(m_Plant.m_bullet1s[j].m_Free)
            {
                continue;
            }

            if(m_conezombs[i].m_Rect.intersects(m_Plant.m_bullet1s[j].m_Rect))
            {

                m_Plant.m_bullet1s[j].m_Free=true;
                m_conezombs[i].m_HP-=bullet.m_injury;
                if(m_conezombs[i].m_HP<=0){
                    m_conezombs[i].m_Free=true;
                    m_Plant.m_score+=2;
                    m_conezombs[i].m_HP=2;
                    for(int k=0;k<BOMB_NUM;k++){
                        if(m_bombs[k].m_Free)
                        {
                            m_bombs[k].m_Free=false;

                            m_bombs[k].m_X=m_conezombs[i].m_X;
                            m_bombs[k].m_Y=m_conezombs[i].m_Y;
                            break;
                        }

                    }
                }
            }
        }

    }
    for(int i=0;i<BUCKETZOMB_NUM;i++)
    {
        if(m_bucketzombs[i].m_Free)
        {
            continue;
        }
        for(int j=0;j<BULLET1_NUM;j++)
        {
            if(m_Plant.m_bullet1s[j].m_Free)
            {
                continue;
            }

            if(m_bucketzombs[i].m_Rect.intersects(m_Plant.m_bullet1s[j].m_Rect))
            {

                m_Plant.m_bullet1s[j].m_Free=true;

                m_bucketzombs[i].m_HP-=bullet.m_injury;
                if(m_bucketzombs[i].m_HP<=0){
                    m_bucketzombs[i].m_Free=true;
                    m_Plant.m_score+=3;
                    m_bucketzombs[i].m_HP=3;
                    for(int k=0;k<BOMB_NUM;k++){
                        if(m_bombs[k].m_Free)
                        {
                            m_bombs[k].m_Free=false;

                            m_bombs[k].m_X=m_bucketzombs[i].m_X;
                            m_bombs[k].m_Y=m_bucketzombs[i].m_Y;
                            break;
                        }

                    }
                }
            }
        }

    }
}

void mainScence::end()
{
    for(int i = 0;i < COMMONZOMB_NUM;i++)
    {
        if(m_commonzombs[i].m_Free == true)
        {
            return;
        }

        if(m_commonzombs[i].m_X <= 0)
        {
            m_Timer.stop();
            OVER=true;
            update();
        }
    }
    for(int i = 0;i < CONEZOMB_NUM;i++)
    {
        if(m_conezombs[i].m_Free == true)
        {
            return;
        }

        if(m_conezombs[i].m_X <= 0)
        {
            m_Timer.stop();
            OVER=true;
            update();
        }
    }
    for(int i = 0;i < BUCKETZOMB_NUM;i++)
    {
        if(m_bucketzombs[i].m_Free == true)
        {
            return;
        }

        if(m_bucketzombs[i].m_X <= 0)
        {
            m_Timer.stop();
            OVER=true;
            update();
        }
    }

}

void mainScence::win()
{
    if(m_Plant.m_score>=50)
    {
        m_Timer.stop();

        WON=true;
        update();
    }
}

void mainScence::speedup()
{
    if(m_Plant.m_score>=100)
    {
        m_COM.m_speed+=0.5;
        m_CON.m_speed+=0.5;
        m_BUC.m_speed+=0.5;
        m_recorder1*=0.5;
        m_recorder2*=0.5;
        m_recorder3*=0.5;
        m_bullet1.m_injury+=1;
        m_Plant.m_interval-=20;
    }
}

void mainScence::mouseMoveEvent(QMouseEvent *event)
{
    int x=event->x()-m_Plant.m_Rect.width()*0.5;
    int y=event->y()-m_Plant.m_Rect.height()*0.5;

    //边界检测
    if(x<=0){
        x=0;
    }
    if(x>=GAME_WIDTH-m_Plant.m_Rect.width()){
        x=GAME_WIDTH-m_Plant.m_Rect.width();
    }
    if(y<=0){
        y=0;
    }
    if(y>=GAME_HEIGHT-m_Plant.m_Rect.height()){
        y=GAME_HEIGHT-m_Plant.m_Rect.height();
    }

    m_Plant.setposition(x,y);
}


